home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Sample Code / Development Tools & Languages / AppsToGo / pbClock / DoEvent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  5.7 KB  |  237 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        DoEvent.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.defs.h"        /* Get various application definitions.            */
  27. #include "App.protos.h"        /* Get the prototypes for application.            */
  28.  
  29. #ifndef __CTLHANDLER__
  30. #include "CtlHandler.h"
  31. #endif
  32.  
  33. #ifndef __DESK__
  34. #include <Desk.h>
  35. #endif
  36.  
  37. #ifndef __DISKINIT__
  38. #include <DiskInit.h>
  39. #endif
  40.  
  41. #ifndef __ERRORS__
  42. #include <Errors.h>
  43. #endif
  44.  
  45. #ifndef __MENUS__
  46. #include <Menus.h>
  47. #endif
  48.  
  49. #ifndef __TEXTEDITCONTROL__
  50. #include "TextEditControl.h"
  51. #endif
  52.  
  53. #ifndef __TEXTSERVICES__
  54. #include "TextServices.h"
  55. #endif
  56.  
  57. #ifndef __TOOLUTILS__
  58. #include <ToolUtils.h>
  59. #endif
  60.  
  61. #ifndef __UTILITIES__
  62. #include "Utilities.h"
  63. #endif
  64.  
  65.  
  66.  
  67. /*****************************************************************************/
  68.  
  69.  
  70.  
  71. extern Cursor    *gCursorPtr;    /* See the file Window.c for comments about this global. */
  72.  
  73.  
  74.  
  75. /*****************************************************************************/
  76. /*****************************************************************************/
  77.  
  78. #ifdef applec
  79. #pragma segment DoEvent
  80. #endif
  81.  
  82. /*****************************************************************************/
  83. /*****************************************************************************/
  84.  
  85.  
  86.  
  87. /* Do the right thing for an event.  Determine what kind of event it is, and
  88. ** call the appropriate routines. */
  89.  
  90. void    DoEvent(EventRecord *event)
  91. {
  92.     WindowPtr    window, oldPort;
  93.     Point        pt;
  94.     OSErr        err;
  95.     TEHandle    teHndl;
  96.     Rect        rct;
  97.  
  98.     switch(event->what) {
  99.  
  100.         case nullEvent:
  101.             DoIdleTasks(event);
  102.             break;
  103.  
  104.         case mouseDown:
  105.             DoMouseDown(event);
  106.             break;
  107.  
  108.         case autoKey:
  109.         case keyDown:                    /* Check for menukey equivalents. */
  110.             DoKeyDown(event);
  111.             break;
  112.  
  113.         case activateEvt:
  114.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  115.             DoActivate((WindowPtr)event->message);
  116.             if (TSMTEAvailable()) TSMEvent(event);
  117.             break;
  118.  
  119.         case updateEvt:
  120.             DoUpdate((WindowPtr)event->message);
  121.             break;
  122.  
  123.         case kHighLevelEvent:
  124.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  125.             DoHighLevelEvent(event);
  126.             break;
  127.  
  128.         case osEvt:
  129.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  130.             switch ((event->message >> 24) & 0xFF) {
  131.                     /* Must logical and with 0xFF to get only low byte. */
  132.                     /* High byte of message. */
  133.  
  134.                 case mouseMovedMessage:
  135.                     DoIdleTasks(event);
  136.                     break;
  137.  
  138.                 case suspendResumeMessage:
  139.                         /* Suspend/resume is also an activate/deactivate. */
  140.                     gInBackground = !((event->message) & resumeFlag);
  141.                     CTEConvertClipboard((event->message) & convertClipboardFlag, !gInBackground);
  142.                     DoActivate(FrontWindow());
  143.                     HiliteWindows();
  144.                     break;
  145.             }
  146.             break;
  147.  
  148.         case diskEvt:
  149.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  150.             if (HiWord(event->message) != noErr) {
  151.                 SetPt(&pt, kDILeft, kDITop);
  152.                 err = DIBadMount(pt, event->message);
  153.             }
  154.             break;        /* It is not a bad idea to at least call DIBadMount
  155.                         ** in response to a diskEvt, so that the user can
  156.                         ** format a floppy. */
  157.     }
  158.  
  159.     DoCursor();
  160.     DoAdjustMenus();
  161.  
  162.     window = CTETargetInfo(&teHndl, &rct);
  163.     if (window) {
  164.         if (rct.left < -8192) {
  165.             GetPort(&oldPort);
  166.             SetPort(window);
  167.             SetOrigin(-16384, 0);
  168.             CTEIdle();
  169.             SetOrigin(0, 0);
  170.             SetPort(oldPort);
  171.         }
  172.         else {
  173.             BeginContent(window);
  174.             CTEIdle();
  175.             EndContent(window);
  176.         }
  177.     }
  178. }
  179.  
  180.  
  181.  
  182. /*****************************************************************************/
  183.  
  184.  
  185.  
  186. /* •• Called by DTS.Lib..framework. •• */
  187.  
  188. /* This is called when a window is activated or deactivated. */
  189.  
  190. void    DoActivate(WindowPtr window)
  191. {
  192.     RgnHandle    rgn, oldClip;
  193.     Point        pt;
  194.  
  195.     NotifyCancel();
  196.  
  197.     if (IsAppWindow(window)) {
  198.  
  199.         SetPort(window);
  200.         DoCtlActivate(window);
  201.         DoDrawFrame(window, true);            /* Redraw frame for new activate state. */
  202.  
  203.         CopyRgn(((WindowPeek)window)->contRgn, rgn = NewRgn());
  204.         DiffRgn(rgn, ((WindowPeek)window)->updateRgn, rgn);
  205.             /* Don't draw any part that's already destined to draw due to an update event.
  206.             ** This prevents part of an exposed window from drawing twice, and thus avoids
  207.             ** flickering. */
  208.  
  209.         BeginContent(window);
  210.  
  211.         pt.h = pt.v = 0;
  212.         GlobalToLocal(&pt);
  213.         OffsetRgn(rgn, pt.h, pt.v);
  214.             /* This offset may seem wrong, since the origin may not be 0,0.  It is actually correct,
  215.             ** since at the time of the GlobalToLocal, the port's origin was set to the content origin.
  216.             ** A clearer way to look at this is in two steps:
  217.             ** 1) With the port's origin at 0,0, do a GlobalToLocal on 0,0, and then offset the new clip
  218.             **    by that amount.
  219.             ** 2) Once the port's origin is set correctly, the new clip needs to be offset again, based
  220.             **    on the origin value.  (The clipRgn travels with the origin, unlike the visRgn.)
  221.             ** The above is what automatically happens when you do the GlobalToLocal of 0,0 on the
  222.             ** correct origin.  Offsets are additive.  One step -- no muss, no fuss. */
  223.  
  224.         GetClip(oldClip = NewRgn());
  225.         SetClip(rgn);
  226.         DoDrawControls(window, true);        /* Redraw content scrollbars for new activate state. */
  227.         SetClip(oldClip);
  228.         DisposeRgn(oldClip);
  229.         DisposeRgn(rgn);
  230.  
  231.         EndContent(window);
  232.     }
  233. }
  234.  
  235.  
  236.  
  237.